import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { commonDetailPayload, getCompanyID, getPosIDs, getTenantID, getUserDepID, getUserID, requestFunComponFiex } from '@/utils';
import { useState } from 'react';
import { useRequest } from 'umi';

const baseUrl = getBaseUrl();

/**
 * 添加模板
 * @param callback 接口回调
 * @returns
 */
const addTempInfoService = (callback: (res: any, clickType: boolean) => void) => {
  const [isPublishType, setIsPublishType] = useState<boolean>(false);

  const addTempInfo = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        createrID: getUserID(),
        createPositionID: getPosIDs(),
        createDepartmentID: getUserDepID(),
        createCompanyID: getCompanyID(),
        ownerID: getUserID(),
        ownPositionID: getPosIDs(),
        ownDepartmentID: getUserDepID(),
        ownCompanyID: getCompanyID(),
      };
      if (bodyParams?.isPublish) {
        setIsPublishType(true);
        delete bodyParams['isPublish'];
      }
      let reqUrl = `${baseUrl}/contractTemplateController/add`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, isPublishType);
      },
    },
  );
  return {
    addTempInfoRun: addTempInfo?.run,
    addTempInfoLoading: addTempInfo?.loading,
  };
};

/**
 * 编辑模板
 * @param callback 接口回调
 * @returns
 */
const editTempService = (callback: (res: any, clickType: boolean) => void) => {
  const editTemp = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractTemplateController/edit`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, false);
      },
    },
  );
  return {
    editTempRun: editTemp?.run,
    editTempLoading: editTemp?.loading,
  };
};

/**
 * 获取详情
 * @param callback  获取详情回调
 * @returns
 */
const getTempByIdService = (callback: (res: any) => void) => {
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}`, //范本状态
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_TYPE}`, //范本类型
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //范本类型
  ];

  const getTempById = useRequest(
    (params: any) => {
      let personUrl = `${baseUrl}/contractTemplateController/getMcontractTemplateByID`;
      const payload: Record<string, any> = {
        ...params,
        dicTypeIds: loadMdicIds?.toString(),
        dicTransName: 'state,businessType,templateType',
        orgTransName: `${ISearchTranslateAll.SIGN_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT}`,
      };
      return requestFunComponFiex('PUT', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    getTempByIdRun: getTempById?.run,
    getTempByIdLoading: getTempById?.loading,
    getTempByIdInfo: getTempById?.data?.obj,
  };
};

/**
 * 签发
 * @param callback 接口回调
 * @returns
 */
const signTempService = (callback: (res: any) => void) => {
  const signTemp = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        signerID: getUserID(),
        signPositionID: getPosIDs(),
        signDepartmentID: getUserDepID(),
        signCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractTemplateController/sign`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    signTempRun: signTemp?.run,
    signTempLoading: signTemp?.loading,
  };
};

/**
 * 在线编辑
 * @param callback 接口回调
 * @returns
 */
const saveToURLWordService = (callback: (res: any, clickType: boolean) => void) => {
  const saveToURL = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractController/saveToURL`;
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: (res: any) => {
        callback(res, true);
      },
    },
  );
  return {
    wordSaveToURLRun: saveToURL?.run,
    wordSaveToURLLoading: saveToURL?.loading,
  };
};

/**
 * 修订
 * @param callback 接口回调
 * @returns
 */
const reviseTempService = (callback: (res: any) => void) => {
  const reviseTemp = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
        modifyerID: getUserID(),
        modifyPositionID: getPosIDs(),
        modifyDepartmentID: getUserDepID(),
        modifyCompanyID: getCompanyID(),
      };
      let reqUrl = `${baseUrl}/contractTemplateController/revise`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    reviseTempRun: reviseTemp?.run,
    reviseTempLoading: reviseTemp?.loading,
  };
};

/**
 * 删除
 * @param callback 接口回调
 * @returns
 */
const deleteTempService = (callback: (res: any) => void) => {
  const deleteTemp = useRequest(
    (payload: any) => {
      let bodyParams = {
        ...payload,
      };
      let reqUrl = `${baseUrl}/contractTemplateController/delete`;
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );
  return {
    deleteTempRun: deleteTemp?.run,
    deleteTempLoading: deleteTemp?.loading,
  };
};

export { addTempInfoService, editTempService, getTempByIdService, signTempService, saveToURLWordService, reviseTempService, deleteTempService };
